<?php

class User_Action_Admin_Manage_Add  extends Action_Admin
{
	function execute($request, $initial = false, $script = null)
	{
		if ($initial = true)
		{
			Agilet::setVar('breadcrum', array(
				array(
					'url' => 'admin/user',
					'label' => 'User'
				),
				array(
					'url' => 'admin/user/manage',
					'label' => 'Manage'
				),
				array(
					'url' => 'admin/user/manage/add',
					'label' => 'Add'
				),
			));
		}

		$form = Form::factory('user/admin/edit');

		$table = Model::getTable('user');

		if ($request -> isGet())
		{

		}

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$data = $form->getData();
			
			$user = $table -> fetchNew();
			
			$value = $form -> getData();
			$user->fromArray($data);
			$user -> created_ip = $request -> getClientIp();
			$user->setPassword($data['password']);
			$user -> save();

			Response::redirect('admin/user/manage');

			$form -> setNotice('Saved.');
		}
		$request -> response = View::render($script = 'core/html/admin/edit', $data = array(
			'form' => $form,
			'title' => 'Edit User'
		));
	}

}
